<template>
	<view class="container">
		<view class="search-bar">
			<UniSearchBar :placeholder="`${$t('obsoleteOutbound.search.placeholder')}`" v-model="keyword" @confirm="search"
				:foucs="numFocus"
				@blur="formblur"
				@input="forminput"
				@cancel="cancel" cancel-text="cancel">
			</UniSearchBar>
		</view>
		<view class="list">
			<uni-list>
				<uni-list-item v-for="(item, index) in businessList" :key="index" showArrow :title="`${$t('obsoleteOutbound.item.outboundNo')}: ${item.outboundNo}  
				${$t('obsoleteOutbound.item.businessNo')}: ${item.businessNo} 
				 `" :note="`${$t('obsoleteOutbound.item.customerName')}: ${item.customerName}`"
					:rightText="`${$t('obsoleteOutbound.item.totalNum')}: ${item.totalNum}`" link="navigateTo"
					:to="`/pages/obsoleteoutbounddetail/obsoleteoutbounddetail?id=${item.id}`" />
			</uni-list>
			<uni-load-more :status="status" :icon-size="14" :content-text="contentText" @clickLoadMore="loadmore" />
		</view>
	</view>
</template>
<script>
import api from '../../common/api.js'
import KeyboardListener from 'uniapp-keyboard-listener'
import UniSearchBar from '@/components/uni-search-bar/uni-search-bar.vue'

export default {
	components: {
		UniSearchBar,
		KeyboardListener
	},
	data() {
		return {
			status: 'more',
			contentText: {
				contentdown: this.$t('load.more.contentdown'),     //'查看更多',
				contentrefresh: this.$t('load.more.contentrefresh'),  //'加载中',
				contentnomore: this.$t('load.more.contentnomore')   // '没有更多了'
			},
			pageIndex: 1,
			baseUrl: '',
			keyword: '',// 报损单号 
			businessList: [],
			code: '',
			numFocus: false,
			pdacode: ''
		}
	},
	onNavigationBarButtonTap(e) {
		console.log(e, '----e---')
			let that = this
		uni.scanCode({
			success: async function (res) {
				console.log('条码类型：' + res.scanType);
				console.log('条码内容：' + res.result);
				that.code = res.result
				await that.findByCode()
			}
		});
	},
	onLoad() {
	
	},
	methods: {
		forminput(){
			this.pdacode = ''
		},
		formfocus() {
			this.pdacode = ''
			setTimeout(() => {
				this.isFocus = false
				this.numFocus = true
			}, 0)
		},
		formblur() {
			// console.log(e.detail.value)
			this.pdacode = ''
			this.numFocus = false;
		},
		async scancodelisten(e) {
			let keyCode = e.keyCode
			// console.log(e, '-----e---')
			if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122)) {
				console.log('字母输入:', e.key);
				this.pdacode = this.pdacode + e.key
			} else if (keyCode >= 48 && keyCode <= 57) {
				console.log('数字输入:', e.key);
				console.log(this.numFocus, 'numFocus');
				this.pdacode = this.pdacode + e.key
			} else if (keyCode === 13) {
				this.code = this.pdacode
				if(this.code!==''){
					await this.findByCode()
				}
				this.pdacode = ''
			} else {
				console.log('其他输入:', e.key);
			}
		
			console.log(this.pdacode, '-----this.pdacode---')
		},
		async findByCode() {
			let ret = await api.getOutboundDetailPage({
				outboundNo: '',
				page: { pageNumber: 1, pageSize: 50 },
				outboundOrderId: this.id
			})
			if (ret.code === 0 && ret.data && ret.data.items.length>0) {
				uni.navigateTo({
					url: `/pages/obsoleteoutbounddetail/obsoleteoutbounddetail?id=${ret.data.items[0].outboundOrderId}`
				})
			} else {
				this.$nextTick(()=>{
					uni.showToast({
						title: this.$t('scanfun.fail'),
						icon: 'none'
					});
				})
				
			}
		},
		async onShow() {
			await this.getPage();
		},
		async getPage() {
			let ret = await api.getOutboundPage({
				keyword: '', page: { pageNumber: this.pageIndex, pageSize: 50 }, type: 4
			})
			console.log(ret, '======ret===page')
			if (ret.code === 0 && ret.data.items.length > 0) {
				this.businessList = ret.data.items
			}
		},
		async cancel() {
			this.keyword = ''
			this.pageIndex = 1
			await this.getPage();
		},
		async search(e) {
			this.keyword = e.value
			this.pageIndex = 1
			let ret = await api.getOutboundPage({ keyword: this.keyword, page: { pageNumber: this.pageIndex, pageSize: 50 }, type: 4 })
			if (ret.data.items.length > 0) {
				this.businessList = ret.data.items
			}
		},
		async scrolltolower() {
			console.log('scrolltolower-------scrolltolower---')
			await this.loadmore();
		},
		toStroeDetail(id){
			console.log('toStroeDetail----toStroeDetail---',id)
			let url = `/pages/storedetail/index?id=${id}`
			console.log('toStroeDetail----toStroeDetail---',url)
			uni.navigateTo({
				url:url
			})
		},
		async loadmore() {
			uni.showLoading({ title: 'Loading...', mask: true })
			this.pageIndex++
			let ret = await api.getOutboundPage({ keyword: this.keyword, page:{pageNumber: this.pageIndex,pageSize:50},type: 4  })
			if(ret.code===0) {
				if(ret.data.items.length>0){
					this.businessList = [].concat(this.businessList, ret.data.items)
				}else{
					this.pageIndex--
				}
			}
			uni.hideLoading()
		},
	}
}
</script>

<style>
.container {
	height: 100vh;
}

.search-bar {
	padding: 10px;
	/* 根据实际高度调整，示例假设搜索栏高度约50px */
	flex-shrink: 0;
	margin-left: 10rpx;
	margin-right: 20rpx;
}

.title-item {

	padding-bottom: 10rpx;
	padding-top: 10rpx;
}
</style>